package com.ruoyi.common.utils;

import com.ruoyi.project.dict.zi.domain.Zi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class ZiUtil {

    private static final Logger log = LoggerFactory.getLogger(ZiUtil.class);

    public static String format(String baike) {
        String s = String.format("<p class=\"f\"></p>\n" +
                "<ul>\n" +
                "    <li><u>%s</u></li>\n" +
                "</ul>\n", baike);
        //log.info(s);
        return s;
    }

    public static String format(String pinyin, String baike) {

        String py = parsePinyin(pinyin);
        if (StringUtils.isEmpty(py)) {
            return String.format("<p class=\"f\"></p>\n" +
                    "<ul>\n" +
                    "    <li><u>%s</u></li>\n" +
                    "</ul>\n", baike);
        } else {
            return String.format("<p class=\"f\"><span class=\"py\">%s</span></p>\n" +
                    "<ul>\n" +
                    "    <li><u>%s</u></li>\n" +
                    "</ul>\n", py, baike);
        }

    }

    private static String parsePinyin(String pinyin) {
        String[] datas = pinyin.split("#");
        List<String> l = new ArrayList<>();
        for (String data : datas) {
            if (StringUtils.isEmpty(data)) {
                continue;
            }
            l.add(data);
        }
        if (l.size() == 1) {
            return l.get(0);
        }
        return "";
    }

    public static String diff(Zi source, Zi dist) {
        StringBuffer sb = new StringBuffer();
        if (!StringUtils.equalsIgnoreCase(source.getBushou(), dist.getBushou())) {
            sb.append(String.format("部首:%s->%s\n", source.getBushou(), dist.getBushou()));
        }
        if (!StringUtils.equalsIgnoreCase(source.getPy(), dist.getPy())) {
            sb.append(String.format("拼音(无声调):%s->%s\n", source.getPy(), dist.getPy()));
        }
        if (!StringUtils.equalsIgnoreCase(source.getPinyin(), dist.getPinyin())) {
            sb.append(String.format("拼音(有声调):%s->%s\n", source.getPinyin(), dist.getPinyin()));
        }
        if (!StringUtils.equalsIgnoreCase(source.getJtz(), dist.getJtz())) {
            sb.append(String.format("简体字:%s->%s\n", source.getJtz(), dist.getJtz()));
        }
        if (!StringUtils.equalsIgnoreCase(source.getFzi(), dist.getFzi())) {
            sb.append(String.format("繁体字:%s->%s\n", source.getFzi(), dist.getFzi()));
        }
        if (!StringUtils.equalsIgnoreCase(source.getWubi(), dist.getWubi())) {
            sb.append(String.format("五笔:%s->%s\n", source.getWubi(), dist.getWubi()));
        }
        if (source.getJiegou() != dist.getJiegou()) {
            sb.append(String.format("结构:%s->%s\n", source.getJiegou(), dist.getJiegou()));
        }
        if (source.getZongbihua() != dist.getZongbihua()) {
            sb.append(String.format("总笔划:%s->%s\n", source.getZongbihua(), dist.getZongbihua()));
        }
        if (source.getBuwaibihua() != dist.getBuwaibihua()) {
            sb.append(String.format("部外笔划:%s->%s\n", source.getBuwaibihua(), dist.getBuwaibihua()));
        }

        if (!StringUtils.equalsIgnoreCase(source.getZhujie(), dist.getZhujie())) {
            sb.append("注解修改(汉典解释):\n");
        }

        return sb.toString();
    }

    public static void main(String[] args) {
        String pinyin = "#b#";
        String baike = "百科";
        System.out.println(format(pinyin, baike));
    }
}
